import 'dart:convert';
import 'package:bot_toast/bot_toast.dart';
import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter_localizations/flutter_localizations.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:freedom/const/game_const.dart';
import 'package:freedom/entity/user_entity_info.dart';
import 'package:freedom/bloc/assets_manager_cubit.dart';
import 'package:freedom/bloc/assets_manager_state.dart';
import 'package:freedom/page/game_index.dart';
import 'package:freedom/page/mahjong/provider/mahjong_room_info.dart';
import 'package:freedom/util/navigator_provider.dart';
import 'package:flutter_bloc/flutter_bloc.dart' as FlutterBloc;
import 'package:provider/provider.dart';
import 'package:freedom/page/user_login_widget.dart';
import 'package:freedom/util/cache_util.dart';
import 'loading/loading_page.dart';

void main() {
  //debugPaintSizeEnabled = true; // 添加此行以启用调试模式
  //debugPaintBaselinesEnabled = true; // 添加此行以启用调试模式
  // debugPaintLayerBordersEnabled = true; // 添加此行以启用调试模式
  //debugPaintPointersEnabled = true; // 添加此行以启用调试模式
  //debugRepaintRainbowEnabled = true; // 添加此行以启用调试模式
  // 禁用硬件加速
  //debugDisableShadows = true;
  // debugDisablePhysicalShapeLayers = true;
  asyncInit();
}

void asyncInit() async {
  WidgetsFlutterBinding.ensureInitialized();
  if (!kIsWeb) {
    SystemChrome.setPreferredOrientations([
      DeviceOrientation.landscapeLeft,
      DeviceOrientation.landscapeRight,
    ]);
  }
  CacheUtil.getInstance(() {
    Widget? userCurrentPage;
    UserEntityInfo userInfoEntity =
        UserEntityInfo(sysUser: SysUser.init("defalut", "defalut"));
    String? userInfoStr =
        CacheUtil.preferencesInstance!.getString(CacheConst.userInfo);
    if (userInfoStr == null) {
      userCurrentPage = UserLoginWidget();
    } else {
      userInfoEntity = UserEntityInfo.fromJson(Map.castFrom(json.decode(
          CacheUtil.preferencesInstance!.getString(CacheConst.userInfo)!)));
      userCurrentPage = const GameIndex();
    }
    // 1334, 750
    runApp(ScreenUtilInit(
      designSize: const Size(800, 360),
      builder: (context, child) => MultiProvider(
        providers: [
          ChangeNotifierProvider(create: (_) => MahjongRoomInfoProvider()),
          ChangeNotifierProvider(create: (_) => userInfoEntity),
        ],
        child: MaterialApp(
            home: MaterialApp(
          localizationsDelegates: const [
            GlobalMaterialLocalizations.delegate,
            GlobalWidgetsLocalizations.delegate
          ],
          locale: const Locale("en", "US"),
          supportedLocales: const [
            Locale("zh", "CH"), //CH和CN好像都行
            Locale("en", "US")
          ],
          debugShowCheckedModeBanner: false,
          builder: BotToastInit(),
          navigatorKey: NavigatorProvider.navigatorKey,
          navigatorObservers: [
            BotToastNavigatorObserver(),
          ],
          // 创建资源加载的Bloc  创建的时候就去加载数据
          home: FlutterBloc.BlocProvider(
            create: (_) => AssetsManagerCubit()..load(),
            child:
                // 通过状态判断是加载主界面还是加载界面
                FlutterBloc.BlocBuilder<AssetsManagerCubit, AssetsManagerState>(
                    builder: (context, state) {
              return state.isLoading
                  ? const AssetsLoadingPage()
                  : userCurrentPage!;
            }),
          ),
        )),
      ),
    ));
  });
}
